package com.justplay.mobilelocation.UI;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.justplay.mobilelocation.R;
import com.justplay.mobilelocation.bean.QuestAccreditCodeBean;
import com.justplay.mobilelocation.util.SharePreferencesUtil;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.ErrorFunListener;
import com.sunshine.retrofit.interfaces.Success;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.justplay.mobilelocation.util.Config.kefuUrl;
import static com.justplay.mobilelocation.util.Config.shouquanma;

public class SoftwarAccreditCodeActivity extends AppCompatActivity {

    @Bind(R.id.toolbar_title)
    TextView toolbar_title;
    @Bind(R.id.toolbar_right)
    TextView toolbar_right;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.btn_help)
    Button btn_help;
    @Bind(R.id.btn_bugCode)
    Button btn_bugCode;
    @Bind(R.id.btn_contast)
    Button btn_contast;
    @Bind(R.id.et_inputCode)
    EditText et_inputCode;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_softwar_accredit_code);
        ButterKnife.bind(this);
        toolbar_title.setText("授权码");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        getCustomerMsg();






    }

    @OnClick({R.id.btn_help, R.id.btn_bugCode, R.id.btn_contast, R.id.toolbar_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_help:

                AlertDialog.Builder builder = new AlertDialog.Builder(SoftwarAccreditCodeActivity.this);
                builder.setMessage("软件授权码是激活本软件系统的一个激活码，本软件系统是付费软件系统，付费后自动生成授权码发送到您注册本软件系统的手机号码上，然后输入授权码后本软件系统即可授权，只需要知道对方号码即可对此号码随时随地的监控，且被监控者毫不知情").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setCancelable(false);

                builder.create().show();

                break;
            case R.id.btn_bugCode:

                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_uri = Uri.parse(url);
                intent.setData(content_uri);
                startActivity(intent);

                break;
            case R.id.btn_contast:
                startActivity(new Intent(SoftwarAccreditCodeActivity.this, ContactUsActivity.class));

                break;

            case R.id.toolbar_right:
                saveCode();
                break;
        }
    }

    private void getCustomerMsg() {
         url = SharePreferencesUtil.getString(kefuUrl, "", SoftwarAccreditCodeActivity.this);

    }

    public void saveCode() {
        String userName = SharePreferencesUtil.getString("userName", "", SoftwarAccreditCodeActivity.this);

        final String codetrim = et_inputCode.getText().toString().trim();
        new HttpUtil.Builder("users/authorizationForUser")
                .Params("username", userName)
                .Params("authorizationCode", codetrim)
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        QuestAccreditCodeBean questAccreditCodeBean = new Gson().fromJson(model, QuestAccreditCodeBean.class);
                        if (questAccreditCodeBean.getFlag() == 1) {
                            SharePreferencesUtil.putBoolean("havedBuy", true, SoftwarAccreditCodeActivity.this);
                            SharePreferencesUtil.putString(shouquanma, codetrim, SoftwarAccreditCodeActivity.this);
                            Toast.makeText(SoftwarAccreditCodeActivity.this, "授权成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(SoftwarAccreditCodeActivity.this, questAccreditCodeBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }
                }).ErrorEventFunc(new ErrorFunListener() {
            @Override
            public void ErrorFun(Object... values) {
                String value = (String) values[1];
                Toast.makeText(SoftwarAccreditCodeActivity.this, "操作失败", Toast.LENGTH_SHORT).show();

            }
        })
                .post();
    }

}
